package checkers3d.storage;

/**Brenton Dong
 *Last Edited 5:13pm 10/17/2009
 *
 * This class contains methods which can be called to create and add to a
 * log file by passing strings.
 *
 * comment(String) simply appends a new line to the file
 * log(String) appends a dated line to the log file
 * newLog(String) OVERWRITES existing log w/ a new file with a dated first line
 */
import java.io.*;
import java.util.*;
public class Logger {
    
    public static void comment(String message) {

        LinkedList PreContents = new LinkedList();

//Reading the Previous Log File.

        try{
        FileInputStream fstream = new FileInputStream("TestLog.txt");
        DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;

//Here I am adding the Previous contents of the file to a Linked List.

        while ((strLine = br.readLine()) != null){
            PreContents.add(strLine);
        }

        }catch (FileNotFoundException e) {
//if there is no file, it will be made after something is written.
        }catch (Exception e){e.printStackTrace();}

/*now the Linked List will be written into the new file, and the new line
        will be added to it.*/

        ListIterator iterator = PreContents.listIterator();

        try{
            FileWriter fstream = new FileWriter("TestLog.txt");
            BufferedWriter out = new BufferedWriter(fstream);

//Here I am rewriting the previous file's contents.

            while (iterator.hasNext()){
                out.write((String)iterator.next());
                out.newLine();
            }

//now I'm writing the new line

            out.write("\t\t" +message);
            out.close();

        }catch (Exception e) {e.printStackTrace();}

        
    }

//This is does the same thing, but adds a dated Timestamp to the message

    public static void log(String message) {

        LinkedList PreContents = new LinkedList();

//Reading the Previous Log File.

        try{
        FileInputStream fstream = new FileInputStream("TestLog.txt");
        DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;

//Here I am adding the Previous contents of the file to a Linked List.

        while ((strLine = br.readLine()) != null){
            PreContents.add(strLine);
        }

        }catch (FileNotFoundException e) {
//if there is no file, it will be made after something is written.
        }catch (Exception e){e.printStackTrace();}

/*now the Linked List will be written into the new file, and the new line
        will be added to it.*/

        ListIterator iterator = PreContents.listIterator();

        try{
            FileWriter fstream = new FileWriter("TestLog.txt");
            BufferedWriter out = new BufferedWriter(fstream);

//Here I am rewriting the previous file's contents.

            while (iterator.hasNext()){
                out.write((String)iterator.next());
                out.newLine();
            }

            Calendar now = Calendar.getInstance();

//now I'm writing the new line
            out.write(now.get(Calendar.MONTH) + "/" 
                    + now.get(Calendar.DAY_OF_MONTH)
                    + " " + now.get(Calendar.HOUR) + ":"
                    + now.get(Calendar.MINUTE) + "    ");
            out.write("\t" + message);
            out.close();

        }catch (Exception e) {e.printStackTrace();}


    }

//This OVERWRITES the previous file with a new dated one.

    public static void newLog(String message){
        try{
            FileWriter fstream = new FileWriter("TestLog.txt");
            BufferedWriter out = new BufferedWriter(fstream);

            Calendar now = Calendar.getInstance();

//now I'm writing the new line
            out.write(now.get(Calendar.MONTH) + "/"
                    + now.get(Calendar.DAY_OF_MONTH)
                    + " " + now.get(Calendar.HOUR) + ":"
                    + now.get(Calendar.MINUTE) + "    ");
            out.write("\t" + message);
            out.close();

        }catch (Exception e) {e.printStackTrace();}
    }

}
